package com.ydgk.jishuzhan.controller;

import com.ydgk.jishuzhan.beans.Page;
import com.ydgk.jishuzhan.beans.Score;
import com.ydgk.jishuzhan.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/score")
public class ScoreController {


    @Autowired
    private ScoreService scoreService;

    @RequestMapping(value = "/toIndex", method = RequestMethod.GET)
    public String toIndex(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                          @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize,
                          @RequestParam(value= "userId" ,required = false ,defaultValue="1") Integer userId,
                          ModelMap modelMap) {

        Integer pageStart = (pageNo - 1) * pageSize;


        Map<String, Object> map = new HashMap<>();
        map.put("start", pageStart);
        map.put("size", pageSize);
        map.put("userId",userId);

        List<Score> scores=scoreService.getScores(map);

        Integer pageTotal = 0;


        Integer ScoreTotal=scoreService.queryScoreTotal(map);

        if (ScoreTotal % pageSize == 0) {
            pageTotal = ScoreTotal / pageSize;
        } else {
            pageTotal = ScoreTotal / pageSize + 1;
        }

        // 将数据封装到模型中
//        modelMap.addAttribute("messages",messages);
//        modelMap.addAttribute("pageNo",pageNo);
//        modelMap.addAttribute("pageTotal",pageTotal);

        // 构建一个对象，将需要的数据封装到对象中，传入页面时直接传入对象
        Page<Score> page = new Page(scores, pageNo, pageTotal);

        modelMap.addAttribute("page",page);

        int ScoreCount=scoreService.getCount(userId);
        modelMap.addAttribute("ScoreCount",ScoreCount);

        return "score-record";
    }

}
